<template>
    <div>
        <el-table :data="store.getters['cate/getCateList']" border style="width: 100%" ref="menuRef"
            :tree-props="{ children: 'children' }" row-key="id">
            <el-table-column prop="id" label="分类编号" width="180" />
            <el-table-column prop="catename" label="分类名称" width="180" />
            <el-table-column label="图片">
                <template v-slot="item">
                    <div>
                        <img v-if="item.row.img" :src="item.row.img" alt="" srcset="">
                    </div>
                </template>
            </el-table-column>

            <el-table-column prop="status" label="状态">
                <template v-slot="item">
                    <div>
                        {{ item.row.status == 1 ? '启用' : '禁用' }}
                    </div>
                </template>
            </el-table-column>
            <el-table-column label="操作">
                <template v-slot="item">
                    <div>
                        <el-button type="primary" @click="edit(item.row.id)">编辑</el-button>
                        <el-button type="danger" @click="del(item.row.id)">删除</el-button>
                    </div>
                </template>
            </el-table-column>
        </el-table>
    </div>
</template>
<script lang='ts' setup>
import { catedelete } from '@/request/api';
import { ElMessageBox, ElMessage } from 'element-plus'
import { useStore } from 'vuex';
import { defineEmits } from 'vue';
let store = useStore();

store.dispatch('cate/getCatelistActions')
// 删除
const del = (id: number) => {
    ElMessageBox.confirm(
        '确定要删除此数据吗?',
        '警告',
        {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning',
        }
    )
        .then(() => {
            catedelete({ id }).then((res: any) => {
                if (res.code == 200) {
                    // 删除成功
                    ElMessage.success(res.msg)
                    // 重新渲染视图
                    store.dispatch('cate/getCatelistActions');
                } else {
                    // 删除失败
                    ElMessage.error(res.msg)

                }
            })
        })
        .catch(() => {
            ElMessage({
                type: 'info',
                message: '已取消删除',
            })
        })


}

// 调用子传父方法
let emit = defineEmits(['edit']);
// 编辑
const edit = (id: number) => {
    // 通知父元素
    emit('edit',id);
}
</script>
<style scoped lang='less'>
img {
    width: 80px;
    height: 80px;
}
</style>